//
//  MSTopSegment.m
//  MinShengBank
//
//  Created by chen wei on 13-4-18.
//  Copyright (c) 2013年 CNTaiPing. All rights reserved.
//

#import "MSTopSegment.h"
#define startX        10
@interface MSTopSegment ()<UIScrollViewDelegate>
{
    UIScrollView *scrollView;
}
@property(nonatomic, retain)NSMutableArray*  segments;
@property(nonatomic, retain)NSMutableArray*  imageviews;
@end

@implementation MSTopSegment
@synthesize delegate;
@synthesize segments;
@synthesize selectedIndex;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;

        self.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];
        self.frame = frame;
        self.segments = [NSMutableArray array];
        self.imageviews = [NSMutableArray array];
        
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        scrollView.bounces = YES;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        scrollView.userInteractionEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollView];
        [scrollView release];
        
        UIView *tem = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 0.5)];
        tem.backgroundColor = [UIColor colorWithHexString:@"bababa"];
        [scrollView addSubview:tem];
        [tem release];
        
        
    }
    return self;
}



- (void) addSegment:(NSString *)title {
    UIButton* button = [self createButton:title];
    [self.segments addObject:button];
    [scrollView addSubview:button];
}

- (void)addNotselectname:(NSString*)notname select:(NSString*)selename;
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:notname] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selename] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(onButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *bgimga = [[UIImageView alloc]initWithFrame:CGRectMake(30, 5, 8, 8)];
    bgimga.backgroundColor = [UIColor clearColor];
    bgimga.hidden = YES;
    bgimga.image = [UIImage imageNamed:@"红.png"];
    
    [self.imageviews addObject:bgimga];
    [button addSubview:bgimga];
    [bgimga release];
    
    [self addSubview:button];
    [self.segments addObject:button];

}

- (void)setselect:(BOOL)value withinde:(int)index;
{
    ((UIImageView*)[self.imageviews objectAtIndex:index]).hidden = value;
       
    [self setNeedsLayout];
}

- (UIButton*) createButton:(NSString *)title{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHexString:@"4b9ead"] forState:UIControlStateSelected];
    [button setBackgroundImage:nil forState:UIControlStateHighlighted];
    button.titleLabel.font = [UIFont systemFontOfSize:21.0f];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonBG.png"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(onButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (void) layoutSubviews {
    [super layoutSubviews];
    CGFloat posx = startX;
    int i = 0;
    for (UIButton* button in self.segments) {
        i += 1;
        if (isIOS7) {
            button.frame = CGRectMake(posx, 0, 48, 48);
        } else {
            button.frame = CGRectMake(posx, 0, 48, 48);
        }
        if (i == 2) {
            posx = CGRectGetMaxX(button.frame)+87;
        }else{
            posx = CGRectGetMaxX(button.frame)+11;
        }
    }
    
    
}

- (void) onButtonSelected:(UIButton*)button {
    int index = 0;
    for (UIButton* item in self.segments) {
        item.selected = NO;
        if (button == item) {
            if ([self.delegate conformsToProtocol:@protocol(MSTopSegmentDelegete)] &&
                [self.delegate respondsToSelector:@selector(selectedSegmentAtIndex:)]) {
                [self.delegate selectedSegmentAtIndex:index];
            }
        }
        index++;
    }
    [self bringSubviewToFront:button];
    button.selected = YES;
    [self setNeedsLayout];
}

- (void) setSelectedIndex:(int)index {
    selectedIndex = index;
    if (index == 5) {
        [scrollView setContentOffset:CGPointMake(78, 0)];
    }else{
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
    for (int i = 0; i < [self.segments count]; i++) {
        if (i == index) {
            [self bringSubviewToFront:(UIButton*)[self.segments objectAtIndex:i]];
            ((UIButton*)[self.segments objectAtIndex:i]).selected = YES;
        } else {
            ((UIButton*)[self.segments objectAtIndex:i]).selected = NO;
        }
    }
}
@end
